import store from '../store'
import {Message} from 'element-ui'

export function text2audio (text) {
  let src = getAudioUrl(text)
  if (src) {
    let audio = new Audio()
    audio.src = src
    audio.play()
  }
}

function getAudioUrl (text) {
  let voiceType = store.getters.voiceType
  let voiceRole = store.getters.voiceRole
  console.log(voiceType, voiceRole)
  if (voiceType === '会话精灵') {
    if (!voiceRole) {
      Message.warning('请选择对应的语音角色')
      return false
    }
    return 'https://dds.dui.ai/runtime/v1/synthesize?voiceId=' + voiceRole + '&text=' + text + '&speed=1&volume=300&audioType=wav'
  } else if (voiceType === '搜狗') {
    if (!voiceRole) {
      Message.warning('请选择对应的语音角色')
      return false
    }
    return 'https://fanyi.sogou.com/reventondc/synthesis?text=' + text + '&speed=1&lang=zh-CHS&from=translateweb&speaker=' + voiceRole
  } else if (voiceType === '有道') {
    return 'http://tts.youdao.com/fanyivoice?word=' + text + '&le=zh&keyfrom=speaker-target'
  }
  return 'https://dds.dui.ai/runtime/v1/synthesize?voiceId=juan1f&text=' + text + '&speed=1&volume=300&audioType=wav'
}
